package com.szlanyou.thirdsdk.retrofit.client;

import android.app.Activity;

import com.szlanyou.thirdsdk.retrofit.loading.INetLoadingManage;
import com.szlanyou.thirdsdk.retrofit.loading.NetLoadingStateAction;

import java.util.HashMap;
import java.util.Map;
import java.util.Stack;

/**
 * @author 陈章
 * create at 2020/11/6 16:52
 * desc: 用于设置Retrofit的loading框实例
 */
public class LoadingActionManager implements INetLoadingManage {
    private NetLoadingStateAction loadingStateAction;
    /**
     * 记录各个activity的loading实例
     */
    private Map<Class<? extends Activity>,NetLoadingStateAction> loadingStateActionStack = new HashMap<>();
    public LoadingActionManager() {
    }

    @Override
    public void setLoadingAction(NetLoadingStateAction loadingAction) {
        if(loadingAction.getActivity() != null){
            loadingStateActionStack.put(loadingAction.getActivity().getClass(),loadingAction);
        }
        this.loadingStateAction = loadingAction;
    }

    @Override
    public NetLoadingStateAction getLoadingAction() {
        return this.loadingStateAction;
    }


    /**
     * 释放已经销毁的activity的loading实例
     * @param activity
     */
    @Override
    public void releaseLoadingAction(Activity activity) {
        NetLoadingStateAction netLoadingStateAction = loadingStateActionStack.get(activity.getClass());
        if(netLoadingStateAction != null){
            netLoadingStateAction.releaseLoading();
            loadingStateActionStack.remove(activity.getClass());
        }
    }
}